package com.yc;

//import com.yc.config.HelloLoadBalancerConfig;
import com.yc.config.IdGeneratorLoadBalancerConfig;
import com.yc.config.HelloLoadBalancerConfig;
import com.yc.config.IdGeneratorLoadBalancerConfig;
import jakarta.annotation.PostConstruct;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;

import java.util.List;

@EnableFeignClients(basePackages = "com.yc.feign") // 启用 Feign 客户端, 利用动态代理机制, 实现服务调用
@EnableDiscoveryClient //开启服务发现功能
@SpringBootApplication
@LoadBalancerClients(value={
        @LoadBalancerClient(name = "service-hello", configuration = HelloLoadBalancerConfig.class),
        @LoadBalancerClient(name = "service-generateId", configuration = IdGeneratorLoadBalancerConfig.class),
},defaultConfiguration= LoadBalancerClientConfiguration.class)  // loadBalancer启动器starter自带的配置类
@MapperScan("com.yc.dao")
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run( ConsumerApplication.class,args);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostConstruct
    public void printInstances() {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-hello");
        System.out.println("Found instances of service-hello: " + instances.size());
        instances.forEach(instance -> System.out.println(instance.getUri()));
    }
}